package practice;
class Num{
    int i = 0;
}
class Thread1 implements Runnable{
    private Num num;
    public Thread1(Num num){
        this.num = num;
    }
    @Override
    public void run() {
        while (true){
            synchronized (num){
                if(num.i % 2 != 0){
                    try {
                        num.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    System.out.println(Thread.currentThread().getName() + "---" + ++num.i);
                    num.notify();
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Thread2 implements Runnable{
    private Num num;
    public Thread2(Num num){
        this.num = num;
    }
    @Override
    public void run() {
        while (true){
            synchronized (num){
                if(num.i % 2 == 0){
                    try {
                        num.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    System.out.println(Thread.currentThread().getName() + "---" + ++num.i);
                    num.notify();
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Test01 {
    public static void main(String[] args){
        Num num = new Num();
        Thread t1 = new Thread(new Thread1(num),"t1");
        Thread t2 = new Thread(new Thread2(num),"t2");
        t1.start();
        t2.start();
    }
}
